package com.supinfo.youfood.template;

import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.LoadableDetachableModel;

import com.supinfo.food.entities.User;
import com.supinfo.youfood.forms.ConnexionForm;
import com.supinfo.youfood.pages.magasin.view.MagasinView;
import com.supinfo.youfood.pages.user.view.UserView;
import com.supinfo.youfood.session.Session;

/**
 * Template page
 * 
 * @author Keitaro
 */
public class Template extends WebPage {

	/* Panel d'administration */
	private WebMarkupContainer panelConnexionLabel = new WebMarkupContainer("panelConnexionLabel");
	private WebMarkupContainer panelConnexionForm = new WebMarkupContainer("panelConnexionForm");
	private WebMarkupContainer panelMenu = new WebMarkupContainer("panelMenu");

	public Template() {
		
		add(new FeedbackPanel("feedback"));
		
		menuManager();
		formManager();
		initLink();
	}

	private void initLink() {
		Link linkMag = new Link("linkMagasin") {
			@Override
			public void onClick() {
				setResponsePage(new MagasinView());
			}
		};
		Link linkUser = new Link("linkUtilisateur") {
			@Override
			public void onClick() {
				setResponsePage(new UserView());
			}
		};
		panelMenu.add(linkMag);
		panelMenu.add(linkUser);
	}

	/**
	 * Gestion du menu
	 */
	private void menuManager() {
		Session session = (Session) getSession();
		
		add(panelConnexionLabel);
		add(panelConnexionForm);
		panelMenu.setVisible(session.getUser() != null);
		panelConnexionForm.setVisible(session.getUser() == null);
		add(panelMenu);
	}

	/**
	 * Gestion du formulaire
	 */
	@SuppressWarnings("rawtypes")
	private void	formManager() {
		Form form = new ConnexionForm("connexionForm", new CompoundPropertyModel(new LoadableDetachableModel() {
			private static final long serialVersionUID = 1L;
			@Override
			protected Object load() {
				return new User();
			}
		}));
		panelConnexionForm.add(form);
	}
}
